package com.shwread.android.qysw.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shwread.android.qysw.bean.task.TaskActBean.TaskActInfo;
import com.shwread.android.qysw10000243.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 活动任务adapter
 */
public class TaskAdapter extends RecyclerView.Adapter {
    /**
     * 组织任务
     */
    public static final int ORGANIZATION_TASK = 1;
    /**
     * 个人任务
     */
    public static final int PERSONAL_TASK = 2;
    /**
     * 个人任务
     */
    public static final int SEARCH_TASK = 3;

    public static String getTime(long time) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA).format(new Date(time * 1000));
    }

    private List<TaskActInfo> mData;
    /**
     * 条目点击监听
     */
    private ItemListener listener;

    public TaskAdapter(List<TaskActInfo> mData, ItemListener listener) {
        this.mData = mData;
        this.listener = listener;
    }

    /**
     * 设置数据
     */
    public void notifyData(List<TaskActInfo> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == ORGANIZATION_TASK) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_organization_task_list, parent, false);
            return new OrganizationTaskHolder(view);
        } else if (viewType == PERSONAL_TASK) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_parsonal_task_list, parent, false);
            return new PersonalTaskHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        final TaskActInfo taskBean = mData.get(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.itemClick(taskBean);
            }
        });
        if (holder instanceof OrganizationTaskHolder) {
            OrganizationTaskHolder taskHolder = (OrganizationTaskHolder) holder;
            taskHolder.TvTaskName.setText(taskBean.getActivityName());
            if (!"3".equals(taskBean.getIndexType())) {
                taskHolder.llTaskContent.setVisibility(View.VISIBLE);
                String startTime = getTime(taskBean.getStartTime());
                String sendTime = getTime(taskBean.getEndTime());
                taskHolder.tvTaskStartTime.setText(String.format("开始时间：%s", startTime));
                taskHolder.tvTaskEndTime.setText(String.format("结束时间：%s", sendTime));
                taskHolder.tvTaskAddress.setText(String.format("活动地点：%s", taskBean.getSignAddress()));
                switch (taskBean.getActivityStatus()) {//未发布|进行中|已完成|已取消
                    case "2":
                        taskHolder.imgTaskStatus.setImageResource(R.drawable.task_ing);
                        break;
                    case "3":
                        taskHolder.imgTaskStatus.setImageResource(R.drawable.task_complete);
                        break;
                    case "4":
                        taskHolder.imgTaskStatus.setImageResource(R.drawable.task_cancel);
                        break;
                }
            } else {
                taskHolder.llTaskContent.setVisibility(View.GONE);
                taskHolder.imgTaskStatus.setImageResource(R.color.transparent);
            }
        } else if (holder instanceof PersonalTaskHolder) {
            PersonalTaskHolder taskHolder = (PersonalTaskHolder) holder;
            taskHolder.TvTaskName.setText(taskBean.getActivityName());
            String applyTime = getTime(taskBean.getApplyTime());
            taskHolder.tvApplyTaskTime.setText(String.format("申请时间：%s", applyTime));
            switch (taskBean.getActivityStatus()) {
                case "1":
                    taskHolder.imgTaskStatus.setImageResource(R.drawable.supervision);
                    break;
                case "2":
                    taskHolder.imgTaskStatus.setImageResource(R.drawable.supervision_pass);
                    break;
                case "3":
                    taskHolder.imgTaskStatus.setImageResource(R.drawable.supervision_not_pass);
                    break;
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mData.get(position).getListType();
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    private static class OrganizationTaskHolder extends RecyclerView.ViewHolder {

        TextView TvTaskName;
        TextView tvTaskStartTime;
        TextView tvTaskEndTime;
        TextView tvTaskAddress;
        ImageView imgTaskStatus;
        LinearLayout llTaskContent;

        private OrganizationTaskHolder(View itemView) {
            super(itemView);
            TvTaskName = (TextView) itemView.findViewById(R.id.tv_task_name);
            tvTaskStartTime = (TextView) itemView.findViewById(R.id.tv_task_start_time);
            tvTaskEndTime = (TextView) itemView.findViewById(R.id.tv_task_end_time);
            tvTaskAddress = (TextView) itemView.findViewById(R.id.tv_task_address);
            imgTaskStatus = (ImageView) itemView.findViewById(R.id.img_task_status);
            llTaskContent = (LinearLayout) itemView.findViewById(R.id.ll_task_content);
        }
    }

    private static class PersonalTaskHolder extends RecyclerView.ViewHolder {
        TextView TvTaskName;
        TextView tvApplyTaskTime;
        ImageView imgTaskStatus;

        private PersonalTaskHolder(View itemView) {
            super(itemView);
            TvTaskName = (TextView) itemView.findViewById(R.id.tv_task_name);
            tvApplyTaskTime = (TextView) itemView.findViewById(R.id.tv_apply_task_time);
            imgTaskStatus = (ImageView) itemView.findViewById(R.id.img_task_status);
        }
    }

    public interface ItemListener {
        void itemClick(TaskActInfo taskBean);
    }
}

